//
//  XZMemberListVC.m
//  Teecloud
//
//  Created by PC on 2018/11/5.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZMemberListVC.h"
#import "XZMemberContentVC.h"
#import "XZMemberListCell.h"
#import "XZMemberData.h"
@interface XZMemberListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tabView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end
#define MEMBERLISTCELL @"MEMBERLISTCELL"
@implementation XZMemberListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self getGroupMemberList];
}
#pragma mark - Networking
- (void)getGroupMemberList{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:self.groupId forKey:@"groupId"];
    
    kWeakObject(self)
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"a/ajax-api.ashx?$c=TeeyunApp&$m=GetGroupMemberList&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [weakself.tabView.mj_header endRefreshing];
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                [weakself.dataSource removeAllObjects];
                NSArray *newsArray = [XZMemberData mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:RESULT_NAME_Data]];
                [weakself.dataSource addObjectsFromArray:newsArray];
                [weakself.tabView.mj_header endRefreshing];
                if (weakself.dataSource.count == 0) {
                    [weakself.tabView setupEmptyDataText:@"暂无数据" secoundText:@"" buttonTit:@"" verticalOffset:0 emptyImage:NODATAIMG tapBlock:^{
                        
                    } butClick:^{
                        
                    }];
                }
                [weakself.tabView reloadData];
            }else{
                [weakself.tabView.mj_header endRefreshing];
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
        }else{
            [weakself.tabView.mj_header endRefreshing];
            [MBProgressHUD showErrorOrSuc:@"网络异常" toView:weakself.view];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    kWeakObject(self)
    XZMemberListCell *memberListCell = [tableView dequeueReusableCellWithIdentifier:MEMBERLISTCELL];
    XZMemberData *model = self.dataSource[indexPath.row];
    memberListCell.model = model;
    //详情传过来的GroupConfig
    memberListCell.groupConfig = self.model;
    //拨号
    memberListCell.takePhoneBlock = ^(XZMemberData * _Nonnull model) {
        NSString* str=[NSString stringWithFormat:@"tel:%@",model.MobileCode];
        UIWebView* callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]]; [weakself.view addSubview:callWebview];
    };
    //考核
    memberListCell.MemberContentBlock = ^(XZMemberData * _Nonnull model) {
        XZMemberContentVC *memberContentVC = [[XZMemberContentVC alloc]init];
        memberContentVC.title = model.Name;
        memberContentVC.groupId = weakself.groupId;
        memberContentVC.userId = model.UserId;
        memberContentVC.groupConfig = weakself.model;
        [weakself.navigationController pushViewController:memberContentVC animated:true];
    };
    return memberListCell;
}
#pragma mark - setter
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource =[[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (UITableView *)tabView{
    if (!_tabView) {
        _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT-kNavBarAndStatusBarHeight-kBottomSafeHeight) style:UITableViewStylePlain];
        _tabView.delegate = self;
        _tabView.dataSource = self;
        _tabView.rowHeight = 50.0f;
        _tabView.showsVerticalScrollIndicator =false;
        _tabView.separatorStyle =UITableViewCellSeparatorStyleNone;
        [_tabView registerNib:[UINib nibWithNibName:@"XZMemberListCell" bundle:nil] forCellReuseIdentifier:MEMBERLISTCELL];
        _tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getGroupMemberList)];
        [self.view addSubview:_tabView];
    }
    return _tabView;
}
@end
